<?php

namespace Beerus\Modules\DesignerList\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Doraemon\model\Designer as DesignerModel;
use Doraemon\model\Designer\DesignerEquity;
use Doraemon\model\DesignerTeam;
use Doraemon\model\KaDesigner;
use Doraemon\model\Member;
use Doraemon\service\Designer\DesignerService;
use Doraemon\service\Wechat\WechatBasic;
use Doraemon\tools\Log;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Frieza\Modules\Position\Actions\CancelApprove;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Form;
use Doraemon\service\User;
use Doraemon\model\Es\Designer;
use Doraemon\model\DesignerAuth;
use Beerus\Modules\DesignerList\Actions\ChangeMainArea;

/**
 * 设计师列表管理
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     hujingdong  <hujingdong@haohaozhu.com>
 * @license    GPL http://www.haohaozhu.com
 * @link       http://www.haohaozhu.com
 */

class IndexController extends Controller
{
    const PAGE_SIZE = 20;

    const STATUS = [
        0 => '已解绑',
        1 => '已绑定'
    ];

    //修改设计师主要服务地区的白名单权限
    //const WHITE_LIST_INFO = ['zhangzhijie','hujingdong','liuxinhua','zhiyu','zhouyang','liziyan'];


    public function index(Request $request, Content $content)
    {

        $request_data = $request->all();
        $page_size = $request->input('per_page', self::PAGE_SIZE);
        $page = $request->input('page', 1);

        //$condition['ORDER'] = ['id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];

        if (!empty($request_data['uid'])) {
            $condition['uid'] = $request_data['uid'];
        }
        $designer_info = Designer::getDesignerInfo($condition);

        $total = $designer_info['total'];
        $lists = $designer_info['rows'];

        foreach ($lists as &$item) {
            try {
                $isSubscribeOfficialAccount = WechatBasic::isSubscribeOfficialAccount($item['uid']);
            } catch (\Exception $e) {
                $isSubscribeOfficialAccount = false;
            }
            $item['is_subscribe_official_account'] = $isSubscribeOfficialAccount ? '是' : '否';
        }
        unset($item);
        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->setActionClass(DropdownActions::class);

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('uid', '用户UID')->placeholder('请输入uid');
                    });
                    $filter->expand();
                });

                $grid->column('id', 'id');
                $grid->column('uid', '设计师uid');
                $grid->column('nick', '昵称');
                $grid->column("designer_type", '设计师类型')->display(function () {
                    return DesignerAuth::DESIGNER_TYPE[$this->designer_type];
                });
                $grid->column("min_price", '最低设计费');
                $grid->column("max_price", '最高设计费');
                $grid->column("main_service_area", '主要服务地区');

                $grid->column("other_service_area", '其他服务地区');
                $grid->column("glory", '荣耀值');
                $grid->column("glory_level", '荣耀等级');
                $grid->column("ranking_score", '算法打分值');
                $grid->column("last_week_pv", '上周pv数值');
                $grid->column("good_notes_count", '优质note数量');
                $grid->column("last_week_msg", '上周咨询数');
                $grid->column("service_type", '服务类型')->display(function () {
                    $scope_ids = explode(' ', $this->service_type);
                    //格式化设计师的服务类型
                    $scope_names = array_map(function ($item) use ($scope_ids){
                        if (in_array($item['id'], $scope_ids)) {
                            return $item['name'];
                        } else {
                            return null;
                        }
                    }, DesignerModel::SERVICE_SCOPE);
                    $scope_names = array_filter($scope_names);
                    return implode(' ',$scope_names);
                });
                $grid->column("honor", '是否获得过设计师大赛')->display(function () {
                    $honor_word = $this->honor ? "是" : "否";
                    return $honor_word;
                });
                $grid->column("is_subscribe_official_account", '是否关注微信服务号')->display(function () {
                    return $this->is_subscribe_official_account;
                });
                $grid->column("bind_article_num", '主页晒家数');
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    $actions->disableView();
                    //此功能目前仅开放给hujingdong和zhangzhijie
                    if (in_array(Admin::user()->username,['zhangzhijie','hujingdong','liuxinhua','zhiyu','zhouyang','liziyan'])){
                        $actions->add(new ChangeMainArea);
                    }
                });
            }
        );

        $tab = new Tab();
        $title = '设计师列表';
        $tab->add($title, $gridList->render());
        return $content
            ->row($tab)
            ->header($title)
            ->breadcrumb(['text' => $title]);
    }

}
